package findKDistantIndices;

import java.util.*;

public class Solution {
    public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == key) {
                // 把 i-k和i+k都加入到结果中
                for (int j = Math.max(i-k,0); j <= Math.min(i+k,nums.length-1); j++) {
                    set.add(j);
                }
            }
        }
        List<Integer> list = new ArrayList<>();
        for (Integer v: set) {
            list.add(v);
        }
        Collections.sort(list);
        return list;
    }
}
